package jp.sourceforge.akjrcp.ui;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.flvtools.basic.SimpleView;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;



public class EditableFileList extends TableViewer{
	public static Log log=LogFactory.getLog(EditableFileList.class);
	protected ArrayList fileList=new ArrayList();
	private MenuItem moveTop;
	private MenuItem moveBottom;
	private MenuItem moveUp;
	private MenuItem moveDown;
	private MenuItem removeFile;
	
	public class MenuSelection implements SelectionListener{

		public void widgetSelected(SelectionEvent e) {
			if(e.getSource()==addfile){
			// TODO Auto-generated method stub
			
			}else if(e.getSource()==moveTop){
				moveTop();
				}else if(e.getSource()==moveBottom){
				moveBottom();
				}else if(e.getSource()==moveUp){
				moveUp();
				}else if(e.getSource()==moveDown){
				moveDown();
				}else if(e.getSource()==removeFile){
				removeFile();
				}
			//getControl().redraw();
			refresh(true);
		}

		public void widgetDefaultSelected(SelectionEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void removeFile(){
		 log.info("remove");
	
			File files[]=getSelectionFiles();
			for (int i = 0; i < files.length; i++) {
				fileList.remove(files[0]);
			}
			
	}
	public void moveUp(){
		File files[]=getSelectionFiles();
		
		if(files.length==1){
			int index=fileList.indexOf(files[0]);
			fileList.remove(files[0]);
			fileList.add(Math.max(0,index-1),files[0]);
		}
	}
	
	public void moveDown(){
		File files[]=getSelectionFiles();
		if(files.length==1){
			int index=fileList.indexOf(files[0]);
			fileList.remove(files[0]);
			fileList.add(Math.min(fileList.size(),index+1),files[0]);
		}
	}
	
	public void moveTop(){
		File files[]=getSelectionFiles();
		if(files.length==1){
			fileList.remove(files[0]);
			fileList.add(0,files[0]);
		}
	}
	
	public void moveBottom(){
		File files[]=getSelectionFiles();
		if(files.length==1){
			fileList.remove(files[0]);
			fileList.add(files[0]);
		}
	}
	
	public EditableFileList(Composite parent,int style){
		super(parent,style);
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		
		MenuSelection selection=new MenuSelection();
		Menu  menu1 = new Menu(getTable());
	        getTable().setMenu(menu1);
	        addfile = new MenuItem(menu1, SWT.PUSH);
			addfile.setText(Messages.getString("FileListTable.addfile")); //$NON-NLS-1$
	        addfile.addSelectionListener(selection);
	        
	        MenuItem s1=new MenuItem(menu1,SWT.SEPARATOR);
	        moveTop = new MenuItem(menu1, SWT.PUSH);
	        moveTop.setText(Messages.getString("FileListTable.moveTop")); //$NON-NLS-1$
	        moveTop.addSelectionListener(selection);
	        
	        moveBottom = new MenuItem(menu1, SWT.PUSH);
	        moveBottom.setText(Messages.getString("FileListTable.moveBottom")); //$NON-NLS-1$
	        moveBottom.addSelectionListener(selection);
	        
	        moveUp = new MenuItem(menu1, SWT.PUSH);
	        moveUp.setText(Messages.getString("FileListTable.moveUp")); //$NON-NLS-1$
	        moveUp.addSelectionListener(selection);
	        
	        moveDown = new MenuItem(menu1, SWT.PUSH);
	        moveDown.setText(Messages.getString("FileListTable.moveDown")); //$NON-NLS-1$
	        moveDown.addSelectionListener(selection);
	        MenuItem s2=new MenuItem(menu1,SWT.SEPARATOR);
	        
	        removeFile = new MenuItem(menu1, SWT.PUSH);
	        removeFile.setText(Messages.getString("FileListTable.removeFile")); //$NON-NLS-1$
	        removeFile.addSelectionListener(selection);
	        
	        getTable().addKeyListener(new KeyAdapter(){
	        	 public void keyReleased(KeyEvent event) {
	        		
	        	        if(event.keyCode==127){ //DELETE KEY
	        	            removeFile();
	        	            refresh(true);
	        	        }
	        	       
	        	    }
	        });
	        

	        Transfer[] types = new Transfer[] {FileTransfer.getInstance()};
	    	
	    	int operations = DND.DROP_MOVE | DND.DROP_COPY ;
	    	
	    	
	    		
	    	
	    	
	    	
	    	
	    		DropTarget target2 = new DropTarget (getTable(), operations);
	    		target2.setTransfer(types);
	    	
	    		target2.addDropListener(new DropMonitor());
	    		
	    		
	    		
	    		this.getTable().setLinesVisible(true);
	        
	    		
	}
	
	  public class DropMonitor extends DropTargetAdapter{
			public void dragEnter(DropTargetEvent event){
				//System.out.println("drag enter:"+event.data);
			}
			public void dragOver(DropTargetEvent event){
				//System.out.println("drag over:"+event.data);
				}
			public void drop(DropTargetEvent event){
				//System.out.println("drop:"+event.data);
				
				//System.out.println(event.data.getClass().getName());
				String files[]=(String[])event.data;
			
			
				for(int i=0;i<files.length;i++){
					addDragAndDropFile(files[i]);
				}
				
				}
		}
	  
	  
	  public void addDragAndDropFile(String path){
	        File file=new File(path);
	        if(filenameFilter!=null){
	        	if(filenameFilter.accept(file.getParentFile(),file.getName())){
	        		addFile(file);
	        	}
	        }else{
	        	addFile(file);
	        }
	    }
	  
	public EditableFileList(Composite parent) {
		super(parent);
		
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		
	}
	
	protected FilenameFilter filenameFilter;
	private MenuItem addfile;
	

	

	
	
            class ViewContentProvider implements IStructuredContentProvider {
        		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
        			
        		}
        		public void dispose() {
        		}
        		public Object[] getElements(Object parent) {
        			
        			return fileList.toArray();
        		}
        	}
            
        	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
        		public String getColumnText(Object obj, int index) {
        			
        			return ((File)obj).getAbsolutePath();
        		}
        		public Image getColumnImage(Object obj, int index) {
        			return null;
        		}

        	}
	
	
	





	protected void addFile(File file){
		fileList.add(file);
		//fro d&d
		refresh(true);
	}
	


	public FilenameFilter getFilenameFilter() {
		return filenameFilter;
	}
	
	public void setFilenameFilter(FilenameFilter filenameFilter) {
		this.filenameFilter = filenameFilter;
	}
	public File[] getFileList() {
		return (File[])fileList.toArray(new File[0]);
	}
	
	
	public File[] getSelectionFiles() {
		ISelection selection=this.getSelection();
		return (File[]) ((IStructuredSelection)selection).toList().toArray(new File[0]);
	}
	
	
}
